<!DOCTYPE html>
<html>
<head>
	<title>合并链表1</title>
</head>
<body>


<script>
function ListNode () {
    function Node (element) {
        this.element = element;// 存放节点内容
        this.next = null;//下一个的指针
    }
    let head = null,
        length = 0;

    this.append = function (element) {
        let node = new Node(element),
            current;

        if(!head) {
            head = node;
        }else {
            current = head;
            while(current.next) {
                current = current.next;
            }
            current.next = node;
        }

        length++;
        return true;
    }
    this.toString = function () {
        let str = '',
            current = head; 

        while(current) {
            str += current.element;
            current = current.next;
        }

        return str;
    }
    this.getHead = function () {
        return head;
    }

    return head;
}
var linkedList1 = new ListNode();
linkedList1.append(11);
linkedList1.append(12);
linkedList1.append(13);
linkedList1.append(14);
var linkedList2 = new ListNode();
linkedList2.append(21);
linkedList2.append(22);
linkedList2.append(23);
linkedList2.append(24);
/*
 * function ListNode(x){
 *   this.val = x;
 *   this.next = null;
 * }
 */
 
/**
  *
  * @param head ListNode类
  * @param n int整型
  * @return ListNode类
  */
var linkedList0 = new ListNode();
linkedList0.append(1);
function mergeTwoLists(l1 ,  l2) {
    let list = linkedList0.getHead();
    //let list = new ListNode(0)
    let cur = list;
    let head1, head2;
    if(l1.element < l2.element) {
        head1 = l1;
        head2 = l2;
    }else {
        head1 = l2;
        head2 = l1;
    }
    cur.next = head1;

    while(cur.next) {
        cur = cur.next;
    }
    cur.next = head2;

    return list.next

}
console.log(mergeTwoLists(linkedList1.getHead(), linkedList2.getHead()));
</script>
</body>
</html>